package com.yjc.drygoods.ware.controller;

import com.yjc.common.utils.PageUtils;
import com.yjc.common.utils.R;
import com.yjc.drygoods.ware.entity.SupplierEntity;
import com.yjc.drygoods.ware.service.SupplierService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author yjc
 * @create 2022-10-24 19:25
 */
@Slf4j
@RestController
@RequestMapping("ware/supplier")
public class SupplierController {
    @Autowired
    SupplierService supplierService;

    /**
     * 获取所有的供货商信息
     */
    @GetMapping("/get/all/suppliers")
    public R getAllSuppliers() {
        List<SupplierEntity> list = supplierService.list();
        log.info("list:{}", list);
        return R.ok().setData(list);
    }

    /**
     * 获取所有的供货商信息APP
     */
    @GetMapping("/app/get/all/suppliers")
    public R getAllSuppliersAPP() {
        List<SupplierEntity> list = supplierService.list();
        log.info("list:{}", list);
        List<Map<String, Object>> collect = list.stream().map(item -> {
            Map<String, Object> map = new HashMap<>();
            map.put("text", item.getSupplierName());
            map.put("value", item.getId());
            map.put("children", new ArrayList<Object>());
            return map;
        }).collect(Collectors.toList());
        return R.ok().setData(collect);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = supplierService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        SupplierEntity supplier = supplierService.getById(id);

        return R.ok().put("supplier", supplier);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody SupplierEntity supplier) {
        supplierService.save(supplier);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody SupplierEntity supplier) {
        supplierService.updateById(supplier);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        supplierService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }
}
